//
//  SIXPopMenu.m
//  discover
//
//  Created by 张天兵 on 16/5/23.
//  Copyright © 2016年 张天兵. All rights reserved.
//

#import "SIXPopMenu.h"

@interface SIXPopMenu()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UILabel *warningLabel;

@end


@implementation SIXPopMenu

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 显示验证码图片
    [self setValueForShowLabel];
}


- (void)setValueForShowLabel{
    
    NSInteger random0 = arc4random_uniform(10);
    NSInteger random1 = arc4random_uniform(10);
    NSInteger random2 = arc4random_uniform(10);
    NSInteger random3 = arc4random_uniform(10);
    
    NSString *str = [NSString stringWithFormat:@"%ld%ld%ld%ld", random0, random1, random2, random3];
    
    // 设置字符串属性
    // 间距
    NSNumber *randomKern = @(arc4random_uniform(10)+5);
    NSDictionary *attrDict = @{NSKernAttributeName: randomKern};
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString: str attributes:attrDict];
    
    for (NSInteger i = 0; i < 4; i++) {
        UIColor *randomColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];
        UIFont *randomFont = [UIFont systemFontOfSize: arc4random_uniform(30)+15];
        
        [attrString addAttribute:NSFontAttributeName value:randomFont range:NSMakeRange(i, 1)];
        [attrString addAttribute:NSForegroundColorAttributeName value:randomColor range:NSMakeRange(i, 1)];
        // 字体宽度
        [attrString addAttribute:NSStrokeWidthAttributeName value:@((int)arc4random_uniform(5)-2) range:NSMakeRange(i, 1)];
        // 倾斜度
        [attrString addAttribute:NSObliquenessAttributeName value:@((int)arc4random_uniform(3)-1) range:NSMakeRange(i, 1)];
    }
    
    self.showLabel.attributedText = attrString;
    
}

/** 换一张 */
- (IBAction)pictureChangeBtnClick:(UIButton *)sender {
    [self setValueForShowLabel];
}

/**  取消 */
- (IBAction)cancelBtnClick:(UIButton *)sender {
    
    [self.delegate popMenuDidCancelOrSuccess:self];

}

/** 确认 */
- (IBAction)confirmBtnClick:(UIButton *)sender {
    
    if (self.textField.text.length) { //验证码正确
        
        if ([self.textField.text isEqualToString:self.showLabel.text]) {
            [self.delegate popMenuDidCancelOrSuccess:self];
            // 跳转至首页
#warning            // insert code...
            
        }else { //验证码错误
            self.tipLabel.text = @"验证码错误,请重新输入";
            [self setValueForShowLabel];
        }
    }else { //未输入验证码
        self.warningLabel.text = @"输入不能为空";
    }
}

/** 显示菜单 */
+ (instancetype)showInPoint:(CGPoint)point {
    
    SIXPopMenu *menu = [[NSBundle mainBundle] loadNibNamed:@"SIXPopMenu" owner:nil options:nil][0];
    menu.center = point;
    menu.transform = CGAffineTransformMakeScale(0.01, 0.01);
    
    // 添加到主窗口
    [[UIApplication sharedApplication].keyWindow addSubview:menu];
    
    
    return menu;
}

/**  隐藏菜单 */
+ (void)hide {
    
    for (UIView *childView in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([childView isKindOfClass:self]) {
            [childView removeFromSuperview];
        }
    }
}

- (void)startPopAnimation {
    // 弹出动画
    [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:50 initialSpringVelocity:15 options:kNilOptions animations:^{
        self.transform = CGAffineTransformIdentity;
    } completion:nil];
    
    // 弹出键盘
    [self.textField becomeFirstResponder];
    
}

@end
